/**
 * Created with IntelliJ IDEA.
 * User: teddy zhang
 * Date: 2015-04-09
 * Time: 15:38
 */
define(function (require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    require('./rescenterBaseCtrl');
    require('./list/rescenterListCtrl');
    require('./search/rescenterSearchCtrl');

    var rescenter = require('./global/rescenterGlobal');
    

    $.widget('rk.rescenterPanelCtrl', $.rk.rescenterBaseCtrl, {

        _create: function () {

            var me = this;
            var elem = me.element;
            var opt = me.options;
            opt.randomkey = Math.random() + '';


            rk.get('/json/oa_rescenter/index.action', function (data) {
                if (data.status === 0) {
                    data = data.data;
                    me._assemble(data);

                }
            }).done(function () {
                me._setGlobal();
                me._render();
                me._bind();


                me.getPanel().rescenterListCtrl({
                    preProcessor:function(event){
                        me._getToolbarContainer().html('');
                    },
                    postProcessor:function (event,data) {
                        me._getToolbarContainer().html(rk.templateText(require('oa/tmpl/rescenter/tpl_rescenter_auth.tpl'), data));
                    }});
            });


        },

        _assemble: function (data) {
            rescenter.depart = data.depart;
            rescenter.group['dep'].groupId = Number(data.depart.groupId);
            $.each(rescenter.group, function (key, _group) {
                _group['auth'] = data.perm[key];
            });

        },


        _init: function () {
            var me = this;
            var elem = me.element;

        },

        _render: function () {
            var me = this;
            var elem = me.element;
            var panel_HTML = rk.templateText(require('oa/tmpl/rescenter/tpl_rescenter_panel.tpl'),window.SESSION);
            elem.html(panel_HTML);
            elem.on('click','.oa-close-panel',function(){
                rk.oa.closePanel();
            });
        },


        _bind: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;


            me.getPanelHeader().manageDir({
                selector: 'a.file_manage',
                success: function () {
                    me.getPanel().rescenterListCtrl('load');
                },
                error: function () {
                    rk.noticeError(rk.i18n('TWITTERFILE_DIRECTORY_UPDATE_FAILURE'));
                }
            });



            me.getPanelHeader().fileUpload({
                permission:function() {
                    return me._getGroup().upload_setting;
                },
                selector: 'a.file_upload',
                url: function (){
                   return me._getGroup().upload_action;
                },
                fullPath: function(){
                    var nav_path = '';
                    me.getPanelContent().find('ul.file_path_menu>li').each(function (idx, item) {
                        nav_path += idx ? '<span class="sub-directory">' + $(item).text() + '</span>' : '<span class="firsr">' + $(item).text() + '</span>';
                    });
                    return nav_path || rk.i18n('TWITTERFILE_ROOT_DIRECTORY');
                },
                success: function () {
                    me.getPanel().rescenterListCtrl('load');
                },
                error: function () {
                    rk.noticeError(rk.i18n('TWITTERFILE_DIRECTORY_UPDATE_FAILURE'));
                }
            });


            me.getPanelHeader().on('click', '.drop_menu a', function () {
                var _this = this;
                var _titleText = $(_this).text()+'<i class="arrow_down"></i>';
                var _act = $(_this).attr('act');
                me._getPanelHeaderTitle().find('> a').html(_titleText);
                me._setGlobal(_act);
                me.getPanel().rescenterListCtrl();
            });




            me._getSearchContainer().on('click', 'a.search-btn', function (event) {
                var _this = this;
                me._doSearch();
                return false;
            });

            me._getSearchContainer().find('input').on('keyup', function (event) {
                if (event.keyCode == 13) {
                    me._doSearch();
                }
                return false;
            });

            me._getSearchContainer().on('click', 'a.search-close', function (event) {
                var $_item = $(this);
                me._getSearchContainer().find('input').val('');
                $_item.hide();
                return false;
            });

            me.getPanelHeader().on('click', 'span.rescenter-title > a', function (event) {

                $(me).attr("randomkey", opt.randomkey);
                $('#drop_rescenter_title').toggle();
                me.getPanelHeader().find('i.arrow_down').toggleClass('arrow_up');
                me._registerToBodyClick();
                return false;
            });


        },

        _doSearch: function () {
            var me = this;
            var elem = me.elet;
            var opt = this.options;
            var inputValue = $.trim(me._getSearchContainer().find('input').val());
            if (inputValue.length <= 0) {
                rk.noticeError(rk.i18n('GROUP_PLEASE_INPUT_CONDITION'));
                return;
            }
            me.getPanel().rescenterSearchCtrl({
                selector:'a.search-btn',
                key:function(){
                    return inputValue;
                },
                preProcessor:function(event){
                    me._getToolbarContainer().html('');
                    me._getSearchContainer().find('a.search-close').show()

                },
                postProcessor:function (event) {
                    me._getSearchContainer().find('a.search-close').hide();
                    me._getSearchContainer().find('input').val('');
                    me.getPanel().rescenterListCtrl();
                }
            });

            me.getPanel().rescenterSearchCtrl('search');
        },

        _registerToBodyClick: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            $(document.body).on('click.' + opt.randomkey, function (e) {
                var target = $(e.target);
                if ($(target).parents().attr('randomkey') == opt.randomkey || target.attr('randomkey') == opt.randomkey) {
                    return;
                }
                window.setTimeout(function () {
                    $(document.body).off('click.' + opt.randomkey);
                    $('#drop_rescenter_title').hide();
                }, 10);
            });
        }

    });


    return $;

});

